//
// AtaDevice.h
// Usage: Defines the interface for a device representing an ATA disk.
// CreateDate: 2009-05-02
// Author: MaJiuyue
//
#ifndef _ATADEVICE_H_
#define _ATADEVICE_H_

#include "AtaLib.h"

struct PartitionInfo
{
	unsigned char active;
	unsigned char start_head;
	unsigned char start_cylinder_low;
	unsigned char start_cylinder_high;
	unsigned char type;
	unsigned char end_head;
	unsigned char end_cylinder_low;
	unsigned char end_cylinder_high;
	unsigned long start_lba;
	unsigned long size;
};

class AtaDevice
{
private:
	DWORD m_dwController;
	DWORD m_dwDrive;
	unsigned short *ident;
	PartitionInfo m_Partitions[4];

public:
	AtaDevice(DWORD dwController, DWORD dwDrive);
	~AtaDevice() {}

/* Query device's attributes */
public:
	DWORD GetCylinders()		{ return (DWORD)ident[0x01]; }
	DWORD GetHeads()		{ return (DWORD)ident[0x03]; }
	DWORD GetBytesPerTrack()	{ return (DWORD)ident[0x04]; }
	DWORD GetBytesPerSector()	{ return (DWORD)ident[0x05]; }
	DWORD GetSectorsPerTrack()	{ return (DWORD)ident[0x06]; }
	DWORD GetBufferSize()		{ return (DWORD)ident[0x15]; } 

	PartitionInfo GetPartitionInfo(int part)
	{
		return m_Partitions[part];
	}

	void GetModelName(char *buf, int maxLen);
	void GetSerialNumber(char *buf, int maxLen);
	void GetFirmwareRevision(char *buf, int maxLen);

/* Device operation */
public:
	/* Make sure device is ready */
	void DeviceReady();

	bool ReadSector(DWORD lba, int cnt, unsigned char *buf);
	bool WriteSector(DWORD lba, int cnt, unsigned char *buf);

	bool ReadPartitionSector(int part, DWORD lba, int cnt, unsigned char *buf);
	bool WritePartitionSector(int part, DWORD lba, int cnt, unsigned char *buf);
};

#endif

